| Conditions | 1 |
| Paths | 2 |
| Total Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* globals versionCompare, atob, btoa */ |
||
| 15 | function ($state, state, data) { |
||
| 16 | this.initSave = function () { |
||
| 17 | state.player = {}; |
||
| 18 | this.versionControl(); |
||
| 19 | state.init(); |
||
| 20 | $state.go('matter'); |
||
| 21 | }; |
||
| 22 | |||
| 23 | this.save = function () { |
||
| 24 | localStorage.setItem('player', JSON.stringify(state.player)); |
||
| 25 | }; |
||
| 26 | |||
| 27 | this.load = function () { |
||
| 28 | try { |
||
| 29 | let storedPlayer = localStorage.getItem('player'); |
||
| 30 | if (!storedPlayer) { |
||
| 31 | this.initSave(); |
||
| 32 | } else { |
||
| 33 | state.player = JSON.parse(storedPlayer); |
||
| 34 | this.versionControl(); |
||
| 35 | } |
||
| 36 | } catch (err) { |
||
| 37 | alert('Error loading savegame, reset forced.'); |
||
| 38 | this.initSave(); |
||
| 39 | } |
||
| 40 | }; |
||
| 41 | |||
| 42 | this.versionControl = function () { |
||
| 43 | // delete saves older than this version |
||
| 44 | if (state.player.version && versionCompare(state.player.version, '2.1.0') < 0) { |
||
| 45 | state.player = {}; |
||
| 46 | } |
||
| 47 | // we merge the properties of the player with the start player to |
||
| 48 | // avoid undefined errors with new properties |
||
| 49 | state.player = angular.merge({}, data.start_player, state.player); |
||
| 50 | // append an id if it doesn't exist |
||
| 51 | if (!state.player.id) { |
||
| 52 | state.player.id = Math.random().toString().substring(3); |
||
| 53 | } |
||
| 54 | |||
| 55 | // old saves may have outdated reactions, which crash the game |
||
| 56 | for(let react in state.player.reactions){ |
||
| 57 | if(typeof data.reactions[react] === 'undefined'){ |
||
| 58 | delete state.player.reactions[react]; |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | // old saves may have outdated resources, which crash the game |
||
| 63 | for(let resource in state.player.resources){ |
||
| 64 | if(typeof data.resources[resource] === 'undefined'){ |
||
| 65 | delete state.player.resources[resource]; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | }; |
||
| 69 | } |
||
| 70 | ]); |
||
| 71 |